package com.coin900.coin.base.utils;


import com.coin900.coin.base.config.ValidationMessage;
import com.coin900.coin.base.enums.ResponseEnum;
import com.value.vo.Pagination;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Value
 * <p/>
 * Copyright (c) 2012 Value ValueSoft Inc., All rights reserved.
 * <p/>
 * This software is the confidential and proprietary information of
 * ValueSoft Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with ValueSoft.com.
 * <p/>
 * Revision History
 * Date      		Programmer       Notes
 * 13-12-17   	     libo		     Initial
 * *********************************************************************
 */
public class ResponseUtil {

    /**
     * hibernate validation验证错误
     * @param mav
     * @param br
     * @return
     */
    public static ModelAndView buildHvMav(ModelAndView mav, BindingResult br){
        mav.addObject("stat", ResponseEnum.VERIFY_FAIL);
        List<Map<String ,Object>> errors = new ArrayList<Map<String, Object>>();
        mav.addObject("errors",errors);
        for (ObjectError objectError : br.getAllErrors()) {
            Map<String ,Object> error = new HashMap<String, Object>();
            error.put("field", ((FieldError) objectError).getField());
            error.put("msg",objectError.getDefaultMessage());
            errors.add(error);
        }
        return mav;
    }
    /**
     * 验证错误
     * @param mav
     * @param field
     * @param failCode
     * @return
     */
    public static ModelAndView buildVfMav(ModelAndView mav, String field, String failCode){
        mav.addObject("stat", ResponseEnum.VERIFY_FAIL);
        List<Map<String ,Object>> errors = new ArrayList<Map<String, Object>>();
        mav.addObject("errors",errors);
        Map<String ,Object> error = new HashMap<String, Object>();
        error.put("field", field);
        error.put("msg", ValidationMessage.getMessage(failCode));
        errors.add(error);
        return mav;
    }

    /**
     * 成功响应
     * @param mav
     * @return
     */
    public static ModelAndView buildSuccessMav(ModelAndView mav){
        mav.addObject("stat", ResponseEnum.OK);
        return mav;
    }

    /**
     * 成功响应
     * @param mav
     * @return
     */
    public static ModelAndView buildPageMav(ModelAndView mav, Pagination page){
        mav.addObject("stat", ResponseEnum.OK);
        return mav;
    }

    /**
     * 新增集合
     * @param mav
     * @param list
     */
    public static void addCollection(ModelAndView mav, Object list){
        mav.addObject("collection", list);
    }
}
